<?php

namespace app\api\controller;

/**
 * 首页接口
 */
class Disease extends Base
{
    protected $noNeedLogin = ['*'];

    public function get_list2()
    {
        $disease = new \app\common\model\Disease();
        $office_id = $this->request->post('off_id','');
        $keyword = $this->request->post('keyword','');
        $info = [];
        $where=[];
        if($office_id){
            $where['off_id']=$office_id;
            $d_id = db('hkj')->where('off_id',$office_id)->column('d_id');
            $where2= [];
            if($d_id){
                $new_id = array_unique($d_id);
                $where2['disease_id'] = ['in',$new_id];
            }
            $info =$disease->where($where2)->where('name',"like","%$keyword%")->field('disease_id,name,first_name')->order('weight')->select();
        }else{
            $info =$disease->field('disease_id,name,first_name')->where('name',"like","%$keyword%")->order('weight')->select();
        }
        $info=collection($info)->toArray();
        if($info){
            $info = groupByInitials($info,'first_name');
        }
        $this->success('获取成功',$info,200);
    }
    public function get_list()
    {
        $disease = new \app\common\model\Disease();
        $office_id = $this->request->post('off_id','');
        $keyword = $this->request->post('keyword','');
        $info = [];
        $where=[];
        if($keyword){
            $where['name']=["like","%$keyword%"];
        }
        if($office_id){
            $where['office_id']=$office_id;
        }
        $info =$disease->where($where)->field('disease_id,name,first_name')->order('weight')->select();
        $info=collection($info)->toArray();
        if($info){
            $info = groupByInitials($info,'first_name');
        }
        $this->success('获取成功',$info,200);
    }
    //病症详情
    public function detail2()
    {
        //病症id
        $id = $this->request->get("id");
        if(!$id) $this->error('参数错误');
        $dis_model = new \app\common\model\Disease();
        $info = $dis_model->with('hospital')->where('disease_id',$id)->find();
        if(!$info){
            $this->error('病症查询失败');
        }
        $office_ids = db('hkj')->where('d_id',$id)->column('off_id');
        $office_data = [];
        if($office_ids){
            $office_ids = array_unique($office_ids);
            $office_data = db('office')->field('id,type_name')->where('id','in',$office_ids)->select();
        }
        $info->office_info = $office_data;
        $info = json_decode(json_encode($info),true);
        if($info['hospital']){
            $tag_model = new \app\admin\model\cyclopedia\Tag();
            if($info['hospital']['tag']){
                $tag_list = $tag_model->where('id','in',$info['hospital']['tag'])->select();
                $info['hospital']['tag'] = json_decode(json_encode($tag_list),true);
            }
        }
        $this->success('获取成功',$info,200);
    }
    public function detail()
    {
        //病症id
        $id = $this->request->get("id");
        if(!$id) $this->error('参数错误');
        $dis_model = new \app\common\model\Disease();
        $info = $dis_model->with(['hospital','office'])->where('disease_id',$id)->find();
        if(!$info){
            $this->error('病症查询失败');
        }
        $info = json_decode(json_encode($info),true);
        if($info['hospital']){
            $tag_model = new \app\admin\model\cyclopedia\Tag();
            if($info['hospital']['tag']){
                $tag_list = $tag_model->where('id','in',$info['hospital']['tag'])->select();
                $info['hospital']['tag'] = json_decode(json_encode($tag_list),true);
            }
            if($info['hospital']['image']){
                $info['hospital']['image']=cdnurl($info['hospital']['image'], true);
            }
        }
        $this->success('获取成功',$info,200);
    }
}